package com.lcw.citylist.widget;

import java.util.Arrays;
import java.util.List;

import android.widget.SectionIndexer;

/**
 * 这里是分段信息
 * @author xxx
 *
 */
public class ContactsSectionIndexer implements SectionIndexer
{

	private static String OTHER = "★";
	private static String[] mSections = { OTHER, "A", "B", "C", "D", "E", "F",
		"G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
		"T", "U", "V", "W", "X", "Y", "Z" };

	private static int OTHER_INDEX = 0; // index of other in the mSections array

	private int[] mPositions; // store the list of starting position index for
	// each section
	// e.g. A start at index 0, B start at index 20,
	// C start at index 41 and so on

	private int mCount; // this is the count for total number of contacts

	// Assumption: the contacts array has been sorted
	public ContactsSectionIndexer(List<ContactItemInterface> contacts)
	{
		mCount = contacts.size();

		initPositions(contacts);
	}

	/**
	 * 根据拼音首字母 获取 标题头
	 * @param indexableItem 
	 * @return
	 */
	public String getSectionTitle(String indexableItem)
	{
		int sectionIndex = getSectionIndex(indexableItem);
		return mSections[sectionIndex];
	}

	// return which section this item belong to
	private int getSectionIndex(String indexableItem)
	{
		if (indexableItem == null)
		{
			return OTHER_INDEX;
		}

		indexableItem = indexableItem.trim();
		String firstLetter = OTHER;

		if (indexableItem.length() == 0)
		{
			return OTHER_INDEX;
		} else
		{
			// get the first letter
			firstLetter = String.valueOf(indexableItem.charAt(0)).toUpperCase();
		}

		int sectionCount = mSections.length;
		for (int i = 0; i < sectionCount; i++)
		{
			if (mSections[i].equals(firstLetter))
			{
				return i;
			}
		}
		return OTHER_INDEX;
	}

	//这里初始化 position对应的索引
	//
	private void initPositions(List<ContactItemInterface> contacts)
	{
		int sectionCount = mSections.length;
		mPositions = new int[sectionCount];

		Arrays.fill(mPositions, -1); // initialize everything to -1

		//加了头 以后 这里要改成从 1开始
		int itemIndex = 1;

		for (ContactItemInterface contact : contacts)
		{

			String indexableItem = contact.getItemForIndex();
			int sectionIndex = getSectionIndex(indexableItem); // find out which
			// section this
			// item belong
			// to

			if (mPositions[sectionIndex] == -1) // if not set before, then do
				// this, otherwise just ignore
				mPositions[sectionIndex] = itemIndex;

			itemIndex++;

		}

		int lastPos = -1;

		// now loop through, for all the ones not found, set position to the one
		// before them
		// this is to make sure the array is sorted for binary search to work
		for (int i = 0; i < sectionCount; i++)
		{
			if (mPositions[i] == -1)
				mPositions[i] = lastPos;

			lastPos = mPositions[i];
		}

		//这里作以下特殊处理 第一项 对应的是listview头
		mPositions[0] = 0;
	}

	@Override
	public int getPositionForSection(int section)
	{
		if (section < 0 || section >= mSections.length)
		{
			return -1;
		}
		return mPositions[section];
	}

	@Override
	public int getSectionForPosition(int position)
	{
		if (position < 0 || position >= mCount)
		{
			return -1;
		}

		int index = Arrays.binarySearch(mPositions, position);

		/*
		 * Consider this example: section positions are 0, 3, 5; the supplied
		 * position is 4. The section corresponding to position 4 starts at
		 * position 3, so the expected return value is 1. Binary search will not
		 * find 4 in the array and thus will return -insertPosition-1, i.e. -3.
		 * To get from that number to the expected value of 1 we need to negate
		 * and subtract 2.
		 */
		return index >= 0 ? index : -index - 2;
	}

	@Override
	public Object[] getSections()
	{
		return mSections;
	}

	/**
	 * 判断 数据项 是不是 该段 的一个项目
	 * @param position  数据项的索引（在总数据中的索引）
	 * @return
	 */
	public boolean isFirstItemInSection(int position){ 
		//注意：这里要加1，因为位置表中 包含头，数据项实际从第一项开始
		int section = Arrays.binarySearch(mPositions, position + 1);
		return (section > -1);
	}

}
